package org.springboot.web;

 
import org.springboot.user.User;
import org.springboot.user.UserService;
import org.springboot.user.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;  

 

import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
  
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 







import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;  
import java.util.HashMap;
import java.util.List;
import java.util.Map;  

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
  
@Controller  

public class WebContrpller {  
    
	//自动注入
    @Autowired
    private UserService userSerivce ;
    
    private StringBuilder temp = new StringBuilder();
    private StringBuilder DateNull=null;
    
    //请求控制
    @RequestMapping("/")  
    public String web( ){  
        //返回templates目录下的freemaker模板
        return "Add"; 
    }  
    
    @RequestMapping("/Add")
    public String Add() {
        return "Add";
    }
    
    @RequestMapping(value = "/AddComfirm", method = RequestMethod.POST)//post请求
    @ResponseBody
    public void AddComfirm(HttpServletRequest request) {
    	
    	 userSerivce.add(request.getParameter("name"),request.getParameter("code"),request.getParameter("remark"), 
    	 	request.getParameter("state"),request.getParameter("create_time"),request.getParameter("account_class"));
 
    }
    
    @RequestMapping(value = "/DeleteComfirm", method = RequestMethod.POST)
    @ResponseBody
    public void DeleteComfirm(HttpServletRequest request) {
    	 
    	userSerivce.deleteByCode(request.getParameter("deletecode"));
       
    }
    
    @RequestMapping(value = "/AlterComfirm", method = RequestMethod.POST)
    @ResponseBody
    public void AlterComfirm(HttpServletRequest request) {
    	 
     	userSerivce.alterByCode(request.getParameter("code"), request.getParameter("name"), request.getParameter("remark"));

    }
    
    @RequestMapping(value = "/GoQuery", method = RequestMethod.POST,produces="application/json;charset=UTF-8")
    @ResponseBody
    public void  GoQuery(HttpServletRequest request) {
    	 
    	java.util.List<Map<String, Object>> list = null;
    	 //划分不同的查询方式
    	if(request.getParameter("type").equals("1")){
    		list = userSerivce.queryBycode(request.getParameter("code"));
    	}else if(request.getParameter("type").equals("2")){
    		list = userSerivce.queryByname(request.getParameter("name"));
    	}else if(request.getParameter("type").equals("3")){
    		//初始化DATE数据
    		String[] date = {"0","0"};
    		String[] date_temp;
    		//分割字符串
    		date_temp=request.getParameter("date").split("-", 4);
    		date[0]=date_temp[0]+"-"+date_temp[1]+"-"+date_temp[2];
    		date[1]=date_temp[3].trim();
    		//查询
    		list = userSerivce.queryBydate(date);
    		
    	}else if(request.getParameter("type").equals("4")){
    		list = userSerivce.queryByClass(request.getParameter("Class"));
    	} 
    	//如果查询到数据
    	if(list.size()>0){
    		//Map<String, Object>转JSON
    		StringBuilder sb = new StringBuilder();
	        sb.append("{"+"\""+"data"+"\":[");
	        for(Map<String, Object> map:list){
	        	sb.append("{");
	        for (String key : map.keySet()) {
	            sb.append("\"").append(key).append("\":\"").append(map.get(key))
	                    .append("\"").append(",");
	        }
	        sb.deleteCharAt(sb.lastIndexOf(","));
	        sb.append("},");
	}
	        sb.deleteCharAt(sb.lastIndexOf(","));
	        sb.append("]}");
	       
	        DateNull=sb;
	        
    	}else{
    		   DateNull=null;
    	}
    }
    
    @RequestMapping(value = "/GetData", method = RequestMethod.POST)
    @ResponseBody
    public String GetData(HttpServletRequest request) {
    	//datatable从这里获取数据
    	init(); 
    	if(DateNull==null){
    		return temp.toString();
    	}else{
    		return DateNull.toString();
    	}
        
    }
    @RequestMapping(value = "/SetState", method = RequestMethod.POST)
    @ResponseBody
    public void SetState(HttpServletRequest request) {
    	userSerivce.setState(request.getParameter("code"), request.getParameter("state"));
    	
    }
     
    
    @RequestMapping("/Alter")
    public String Alter() {
        return "Alter";
    }
    @RequestMapping("/Delete")
    public String delete() {
        return "Delete";
    }
    @RequestMapping("/Query")
    public String query() {
        return "Query";
    }
    @RequestMapping("/SetState")
    
    public String setstate() {
        return "SetState";
    }
    private void init(){
    	//初始化一个空的JSON返回
    	temp= new StringBuilder();
    	String[] arg={"name","code","remark","state","create_time","last_login_time","account_class"};
		temp.append("{"+"\""+"data"+"\":[");
		temp.append("{");
         for(int i=0;i<arg.length;i++){
        	 temp.append("\"").append(arg[i]).append("\":\"").append("null")
                    .append("\"").append(",");
         }
         temp.deleteCharAt(temp.lastIndexOf(","));
         temp.append("},");
 
         temp.deleteCharAt(temp.lastIndexOf(","));
         temp.append("]}");
    }
    
    
}  